package com.yh.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.bean.Dept;
import com.yh.bean.Desk;
import com.yh.bean.FileSet;
import com.yh.bean.Permission;
import com.yh.bean.Role;
import com.yh.bean.User;
import com.yh.dao.FileDao;
import com.yh.dao.ManageDao;
import com.yh.service.ManageService;

@Service

public class ManageServiceImpl implements ManageService{
	@Autowired
	private ManageDao managedao;
	@Autowired
	private FileDao filedao;
	
	//======================================================================部门管理======
	//新增部门
	@Override
	@Transactional
	public int addDept(Dept dept) {
		int a=managedao.addDept(dept);
		return a;
	}
	
	//查询所有部门信息
	@Override
	public List<Dept> queryListDept(int deptid) {
		List<Dept> listDept=managedao.queryListDept(deptid);
		return listDept;
	}
	
	//修改部门信息
	@Override
	@Transactional
	public int updateDept(Dept dept) {
		int a=managedao.updateDept(dept);
		return 0;
	}
	
	//删除部门
	@Override
	@Transactional
	public int delDept(int deptid) {
		int a=managedao.delDept(deptid);
		return 0;
	}
	//======================================================================角色管理======
	//新增角色
	@Override
	@Transactional
	public int addRole(Role role) {
		int a=managedao.addRole(role);
		return 0;
	}
	
	//删除角色
	@Override
	@Transactional
	public int delRole(int roleid) {
		int a=managedao.delRole(roleid);
		return 0;
	}
	
	//修改角色
	@Transactional
	public int updateRole(Role role){
		
		int a=managedao.updateRole(role);
		
		return 0;
	}
	

	//展示所有角色
	@Override
	public Map queryListRole(int currentpage,int pagesize) {
		PageHelper.startPage(currentpage, pagesize,"role_sort");
		
		List<Role> listRole=managedao.queryListRole();
		PageInfo<Role> pinfo=new PageInfo<Role>(listRole);
		
		Map map=new HashMap();
		map.put("listRole", listRole);
		//map.put("totalpage", pinfo.getPages());
		map.put("totalnum", pinfo.getTotal());
		return map;
	}

	//根据角色id查询所有权限
	@Override
	public List<Permission> queryListPermission(Role role) {
		List<Permission> listPermission=managedao.queryListPermission(role);
		return listPermission;
	}

	//更改权限
	@Transactional
	public int updatePermission(int[] arrperid,String[] arrpername,int roleid){
		
		//根据角色id删除所有权限
		int a=managedao.delPermission(roleid);
		List list=new ArrayList();
		for(int i=0;i<arrperid.length;i++){
			Map map=new HashMap();
			map.put("per_id", arrperid[i]);
			map.put("per_name", arrpername[i]);
			map.put("per_roleid", roleid);
			list.add(map);
		};
		int b= managedao.addPermission(list);
		
				
		
		return b;
	}
	
	//========================================================================文件管理======
	//设置文件大小  和 类型
	@Override
	public int updateFileSet(int size, String[] arr) {
		
		
		
		StringBuffer sbf=new StringBuffer();
		for(int i=0;i<arr.length;i++){
			sbf.append("."+arr[i]);
		}
		System.out.println(sbf);
		int a=filedao.updateFileSet(size, sbf.toString());
		
		
		return 0;
	}
	//查文件类型
	@Override
	public FileSet queryFileSet() {
		
		FileSet fileSet=filedao.queryFileSet();
		return fileSet;
	}
	
	//=============================================================桌面管理=========
	//查询已有桌面
	public List<Desk> queryDesk(){
		
		List<Desk> listDesks=managedao.queryDesk();
		
		return listDesks;
	}
	
	//批量更改desk
	public int updateDesk(List<Desk> listdesk){
		
		int a=managedao.updateDesk(listdesk);
		
		return 0;
	}

}
